package com.kuzlak.cms.libraries;


import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import org.apache.commons.codec.binary.Base64;

import jakarta.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Map;

public class Utils {
    private static Cache<Object, Object> cacheObj = CacheUtil.newFIFOCache(50);
    private static int TYPE = 0;

    public static boolean isCache(String addressCode) {
        return cacheObj.containsKey(addressCode + "qrcode");
    }

    public static boolean setCacheQrCode(String qrcode) {
        String qrCodeDate = getQrCodeDate();

        String substring = qrcode.substring(8, 12);

        int i = Integer.parseInt(substring);

        cacheObj.put(qrCodeDate + "qrcode", i);

        System.out.println(qrcode);
        //   String date = qrcode.substring(4, 12);
        cacheObj.put("date", qrCodeDate);
        return cacheObj.containsKey(qrCodeDate + "qrcode");
    }

    public static String getHostPath(HttpServletRequest request) {
        String host = "";
        host = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort();
        //System.out.println("Utils host:" +host);

        return host;
    }

    public static String getClickKey(String appid) {
        Calendar instance = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int day = instance.get(Calendar.DAY_OF_MONTH);
        int hour = instance.get(Calendar.HOUR);
        int minut = instance.get(Calendar.MINUTE);
        int scond = instance.get(Calendar.SECOND);
        builder.append(appid);
        builder.append(year);
        builder.append(month);
        builder.append(day);
        builder.append(hour);
        builder.append(month);
        builder.append(scond);

        String s = builder.toString();
        String s1 = s.toUpperCase();
        return s1;


    }


    public static boolean getQrcodeValid() {
        String qrCodeDate = getQrCodeDate();
        return cacheObj.containsKey(qrCodeDate + "qrcode");
    }

    public static String getQrcode() {
        //  int qrcode =Integer.getInteger(cacheObj.get("qrcode").toString());
        String qrCodeDate = getQrCodeDate();
        int code = 0;
        if (isQrCodeDate()) {
            String qrcode = cacheObj.get(qrCodeDate + "qrcode").toString();
            code = Integer.parseInt(qrcode);
            code++;
        }
        cacheObj.put(qrCodeDate + "qrcode", code);
        String s2 = String.valueOf(code);
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < (4 - s2.length()); i++) {
            buffer.append(0);
        }
        buffer.append(code);
        //  System.out.println(code);


        StringBuilder builder = new StringBuilder();


        builder.append(qrCodeDate);
        builder.append(buffer);

        String s = builder.toString();
        return s.toUpperCase();


    }

    public static String getQrcodeDate() {
        //  int qrcode =Integer.getInteger(cacheObj.get("qrcode").toString());
        char[] chr = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        String qrCodeDate = getQrCodeDate1();

        int code = RandomUtil.randomInt(100, 999999);
        String s2 = String.valueOf(code);
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < (6 - s2.length()); i++) {
            int i1 = RandomUtil.randomInt(chr.length);
            buffer.append(chr[i1]);
        }
        buffer.append(code);
        //  System.out.println(code);

        StringBuilder builder = new StringBuilder();
        builder.append("GZ");

        builder.append(qrCodeDate);
        builder.append(buffer);

        String s = builder.toString();
        return s.toUpperCase();


    }


    public static String getQrcode2(String addressCode) {
        //  int qrcode =Integer.getInteger(cacheObj.get("qrcode").toString());
        int code = 0;
        if (isQrCodeDate()) {
            String qrcode = cacheObj.get(addressCode + "qrcode").toString();
            code = Integer.parseInt(qrcode);
            code++;
        }
        cacheObj.put(addressCode + "qrcode", code);
        String s2 = String.valueOf(code);
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < (6 - s2.length()); i++) {
            buffer.append(0);
        }
        buffer.append(code);
        //  System.out.println(code);


        StringBuilder builder = new StringBuilder();

        String substring1 = addressCode.substring(0, 4);
        builder.append(substring1);
        //  builder.append(type);
        builder.append(getQrCodeDate());
        builder.append(buffer);

        String s = builder.toString();
        return s.toUpperCase();


    }

    public static String getQrCodeDate() {

        Calendar instance = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        int year = instance.get(Calendar.YEAR);

        int month = instance.get(Calendar.MONTH) + 1;
        String months = (month > 9) ? String.valueOf(month) : "0" + month;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        String days = (day > 9) ? String.valueOf(day) : "0" + day;
        builder.append(year);
        builder.append(months);
        builder.append(days);

        return builder.toString();
    }

    public static String getQrCodeDate1() {

        Calendar instance = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        int year = instance.get(Calendar.YEAR);

        int month = instance.get(Calendar.MONTH) + 1;
        String months = (month > 9) ? String.valueOf(month) : "0" + month;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        String days = (day > 9) ? String.valueOf(day) : "0" + day;
        String s = String.valueOf(year);
        builder.append(s.substring(2, 4));
        builder.append(months);
        //   builder.append(days);

        return builder.toString();
    }

    public static boolean isQrCodeDate() {
        String qrCodeDate = getQrCodeDate();
        if (cacheObj.containsKey("date")) {


            String date = cacheObj.get("date").toString();

            boolean equals = date.equals(qrCodeDate);
            if (!equals) {
                cacheObj.put("date", qrCodeDate);
            }
            return equals;
        } else {
            cacheObj.put("date", qrCodeDate);
            return false;
        }
    }


    public static String getUser(Map<String, Object> users) {
        Calendar instance = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int day = instance.get(Calendar.DAY_OF_MONTH);
        int hour = instance.get(Calendar.HOUR);
        int minut = instance.get(Calendar.MINUTE);
        int scond = instance.get(Calendar.SECOND);
        int milliscond = instance.get(Calendar.MILLISECOND);
        //QR20212021918104619|3|2200
    /*    builder.append("USER");
        builder.append(year);
        builder.append(year);
        builder.append(month);
        builder.append(day);
        builder.append(hour);
        builder.append(minut);
        builder.append(scond);
        builder.append(milliscond);
        builder.append("|");
        //System.out.println(users);*/
        builder.append(users.get("organId"));
        builder.append("|");
        builder.append(users.get("deptId"));
        builder.append("|");
        builder.append(users.get("deptName"));
        builder.append("|");

        builder.append(users.get("id"));
        builder.append("|");
        builder.append(users.get("name"));
        String encode = encodeBase64String(builder.toString());

        return encode;


    }


    public static String getUser1(Map<String, Object> users) {
        Calendar instance = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        builder.append(users.get("organId"));
        builder.append("|");
        builder.append(users.get("deptId"));
        builder.append("|");
        builder.append(users.get("deptName"));
        builder.append("|");
        builder.append(users.get("id"));
        builder.append("|");
        builder.append(users.get("name"));
        String s = RandomUtil.randomNumbers(6);
        builder.append("|");
        builder.append(s);
        String encode = encodeBase64String(builder.toString());
        cacheObj.put(encode, users.get("id"), DateUnit.MINUTE.getMillis() * 5);
        return encode;


    }


    public static long getUser1Valid(String key) {

        boolean b = cacheObj.containsKey(key);
        if (b) {
            String s = cacheObj.get(key).toString();
            System.out.println("Valid:" + s);
            long l = NumberUtil.parseLong(s);
            return l;
        } else {
            return 0;
        }


    }

    private static String encodeBase64String(String str) {
        Base64 base64 = new Base64();
        byte[] encode = base64.encode(str.getBytes(StandardCharsets.UTF_8));
        return new String(encode);
    }

    public static String quoteReplacement(String s) {
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
            return s;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\\' || c == '$') {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }



    public static String getOrderNum(String serviceType) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric =String.valueOf(RandomUtil.randomInt(100000,900000));
        //5位用户id
        int subStrLength = 5;
    //    String sUserId = userId.toString();
     //   int length = sUserId.length();
        String str="";
   /*     if (length >= subStrLength) {
            str = sUserId.substring(length - subStrLength, length);
        } else {
            str = String.format("%0" + subStrLength + "d", userId);
        }*/
      //  System.out.println(localDate+"---Len:"+localDate.length());
   //     System.out.println(randomNumeric+"---Len:"+randomNumeric.length());
  //      System.out.println(str+"---Len:"+str.length());
        String orderNum = serviceType+localDate + randomNumeric + str;
        orderNum=orderNum.toUpperCase();
     //   System.out.println(orderNum+"---len:"+orderNum.length());
        //    log.info("订单号:{}", orderNum);
        return orderNum;
    }
}